package com.woniuxy.stage3demo.controller.kang;

import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.DeptBean;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.StaffBean;
import com.woniuxy.stage3demo.bean.kang.CustomerBean;
import com.woniuxy.stage3demo.bean.kang.CustomerMsgBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.chenyxuanService.IDeptService;
import com.woniuxy.stage3demo.service.chenyxuanService.IStaffService;
import com.woniuxy.stage3demo.service.kang.ICustomerMsgService;
import com.woniuxy.stage3demo.service.kang.ICustomerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private ICustomerService customerServiceImpl;
    @Resource
    private ICustomerMsgService customerMsgServiceImpl;
    @Resource
    private IDeptService deptServiceImpl;
    @Resource
    private IStaffService staffServiceImpl;

    @GetMapping(value = "/findLimit",produces = {"application/json;charset=utf-8"})
    public ResultMsg findLimit(PageRequest pageRequest){

        PageInfo<CustomerBean> customer = customerServiceImpl.findLimit(pageRequest);
//        System.out.println(customer);

        return new ResultMsg(200,"成功",new TableResult<>(customer.getTotal(),customer.getList()));
    }

    @PutMapping(value = "/statusChange/{id}/{us}",produces = {"application/json;charset=utf-8"})
    public ResultMsg statusChange(@PathVariable("id") Long id, @PathVariable("us") int us){

        CustomerBean customerBean = new CustomerBean();
        customerBean.setId(id);
        customerBean.setStatus(us);
        customerServiceImpl.updateCustomer(customerBean);

        return new ResultMsg(200,"成功",null);
    }

    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg deleteCustomer(@PathVariable("id") Long id){

        customerServiceImpl.deleteCustomer(id);

        return new ResultMsg(200,"成功",null);
    }

    @GetMapping(value = "/getMsg/{id}",produces = {"application/json;charset=utf-8"})
    public Map getMsg(@PathVariable("id") int id){
        HashMap<Object, Object> map = new HashMap<>();

        map.put("countTimes",customerMsgServiceImpl.countByCustomerId(id));
        map.put("countTimesToday",customerMsgServiceImpl.countByCustomerDId(id));
        map.put("countManTimes",customerMsgServiceImpl.countByCUId(id));
        map.put("countManTimesToday",customerMsgServiceImpl.countByCUDId(id));

        return map;
    }

    @GetMapping(value = "/getDept",produces = {"application/json;charset=utf-8"})
    public ResultMsg getDept(){

        List<DeptBean> depts = deptServiceImpl.findAll();

        return new ResultMsg(200,"成功",depts);
    }

    @GetMapping(value = "/getStaff",produces = {"application/json;charset=utf-8"})
    public ResultMsg getStaff(int id){

        List<StaffBean> staff = staffServiceImpl.findAllStaffByDeptId(id);

        return new ResultMsg(200,"成功",staff);
    }

    @PostMapping(value = "/addCustomer",produces = {"application/json;charset=utf-8"})
    public ResultMsg addCustomer(@RequestBody CustomerBean customerBean){

        customerServiceImpl.addCustomer(customerBean);

        return new ResultMsg(200,"成功",null);
    }

    @PutMapping(value = "/modCustomer",produces = {"application/json;charset=utf-8"})
    public ResultMsg modCustomer(@RequestBody CustomerBean customerBean){

        customerServiceImpl.updateCustomer(customerBean);

        return new ResultMsg(200,"成功",null);
    }

    @GetMapping(value = "/getMsgLimit",produces = {"application/json;charset=utf-8"})
    public ResultMsg getMsgLimit(PageRequest pageRequest,int id){
//        System.out.println(pageRequest);
//        System.out.println(id);

        PageInfo<CustomerMsgBean> custs = customerMsgServiceImpl.findByCustomerId(pageRequest, id);
        System.out.println(custs);

        return new ResultMsg(200,"成功",new TableResult<>(custs.getTotal(),custs.getList()));
    }

}
